<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/7
 * Time: 10:27
 */

namespace common\models\Service;


use common\msg\UserMsg;
use common\models\Service\WechatModel;

class WechatService
{

    static public function CreateMenu($post)
    {
        $options = [
            'token' => \Yii::$app->params['token'],
            'encodingaeskey' => \Yii::$app->params['encodingAesKey'],
            'appid' => \Yii::$app->params['appID'],
            'appsecret' => \Yii::$app->params['appSecret'],
            'debug' => true,
        ];

        $wechat = new WechatModel($options);

        $return['status'] = 0;
        $return['msg'] = UserMsg::$time_out;
        if(empty($post)){
            if ($wechat ->deleteMenu()) {
                return ['status' => 1];
            } else {
                $return['msg'] = $wechat->errCode . ":" . $wechat->errMsg;
                return $return;
            }
        }
        if (!is_array($post)) {
            $return['msg'] = "数据错误";
            return $return;
        }
//        echo "<pre>";
//        print_r($post);exit;
        foreach ($post as $key => $val) {
            if (empty($val['name'])) {
                unset($val);
                continue;
            }
            if (isset($val[0]) && is_array($val[0])) {

                foreach ($val[0] as $k => $v) {
                    if (empty($v['name']) || empty($v['val'])) unset($v);
                    else
                        $array = self::setMenu($v);
                    if ($array) {
                        $data[$key]['sub_button'][] = self::setMenu($v);
                    } else {
                        $return['msg'] = "菜单类型为URL跳转时，菜单值必须为网址";
                        return $return;
                    }
                }
                $data[$key]['name'] = $val['name'];
            } else {
                $array = self::setMenu($val);
                if ($array) {
                    $data[$key] = self::setMenu($val);
                } else {
                    $return['msg'] = "数据有误，请检查";
                    return $return;
                }
            }


        }
        $menu['button'] = $data;

//        echo "<pre>";
//        print_r($menu);
//        exit;


        if ($wechat->createMenu($menu)) {

            return ['status' => 1];
        } else {
            $return['msg'] = $wechat->errCode . ":" . $wechat->errMsg;
            return $return;
        }
    }

    static public function setMenu($v)
    {
        $data['type'] = $v['type'];
        $data['name'] = $v['name'];
        if ($v['type'] == 'click') {
            $data['key'] = $v['val'];
            if(empty($v['val'])){
                return false;
            }
        } else if ($v['type'] == 'view') {
            if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/i', $v['val']))
                $data['url'] = $v['val'];
            else
                return false;
        }

        return $data;
    }

}